package com.ruoyi.project.kkad.channelswitch.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.RedisKey;
import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.kkad.channelswitch.domain.ChannelSwitch;
import com.ruoyi.project.kkad.channelswitch.mapper.ChannelSwitchMapper;
import com.ruoyi.project.kkad.channelswitch.service.IChannelSwitchService;

/**
 * 渠道配置Service业务层处理
 * 
 * @author shixiangwen
 * @date 2020-04-13
 */
@Service
@DataSource(DataSourceType.KKAD)
public class ChannelSwitchServiceImpl implements IChannelSwitchService {

	@Autowired
	@Qualifier("commonRedisTemplate")
	RedisTemplate<String, String> redisTemplate;

	@Autowired
	private ChannelSwitchMapper channelSwitchMapper;

	/**
	 * 查询渠道配置
	 * 
	 * @param id 渠道配置ID
	 * @return 渠道配置
	 */
	@Override
	public ChannelSwitch selectChannelSwitchById(Long id) {
		return channelSwitchMapper.selectChannelSwitchById(id);
	}

	/**
	 * 查询渠道配置列表
	 * 
	 * @param channelSwitch 渠道配置
	 * @return 渠道配置
	 */
	@Override
	public List<ChannelSwitch> selectChannelSwitchList(ChannelSwitch channelSwitch) {
		return channelSwitchMapper.selectChannelSwitchList(channelSwitch);
	}

	/**
	 * 新增渠道配置
	 * 
	 * @param channelSwitch 渠道配置
	 * @return 结果
	 */
	@Override
	public int insertChannelSwitch(ChannelSwitch channelSwitch) {
		int ret = channelSwitchMapper.insertChannelSwitch(channelSwitch);
		String key = RedisKey.Ad.getChannelSwithKey();
		String field = channelSwitch.getAppId() + ":" + channelSwitch.getChannel();
		String json = JsonUtil.toJson(channelSwitch);
		redisTemplate.opsForHash().put(key, field, json);
		return ret;
	}

	/**
	 * 修改渠道配置
	 * 
	 * @param channelSwitch 渠道配置
	 * @return 结果
	 */
	@Override
	public int updateChannelSwitch(ChannelSwitch channelSwitch) {
		int ret = channelSwitchMapper.updateChannelSwitch(channelSwitch);
		String key = RedisKey.Ad.getChannelSwithKey();
		String field = channelSwitch.getAppId() + ":" + channelSwitch.getChannel();
		String json = JsonUtil.toJson(channelSwitch);
		redisTemplate.opsForHash().put(key, field, json);
		return ret;
	}

	/**
	 * 删除渠道配置对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteChannelSwitchByIds(String ids) {
		String[] datas = Convert.toStrArray(ids);
		int effect = 0;
		for (String data : datas) {
			long id = Long.parseLong(data);
			if (this.deleteChannelSwitchById(id) > 0) {
				effect += 1;
			}
		}
		return effect;
	}

	/**
	 * 删除渠道配置信息
	 * 
	 * @param id 渠道配置ID
	 * @return 结果
	 */
	@Override
	public int deleteChannelSwitchById(Long id) {
		ChannelSwitch channelSwitch = this.selectChannelSwitchById(id);
		if (channelSwitch != null) {
			String key = RedisKey.Ad.getChannelSwithKey();
			String field = channelSwitch.getAppId() + ":" + channelSwitch.getChannel();
			redisTemplate.opsForHash().delete(key, field);
		}
		return channelSwitchMapper.deleteChannelSwitchById(id);
	}
}
